package linker.window;

import linker.list.*;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */

public class ViewListListener extends MouseAdapter {

	private JList viewList;

	private JPopupMenu lastMenu = null;

	private Point clickPoint = null;

	public ViewListListener(JList viewList) {
		this.viewList = viewList;
		viewList.addMouseListener(this);
	}

	public void mouseExited(MouseEvent e) {
		if (lastMenu != null && clickPoint != null
				&& !e.getPoint().equals(clickPoint))
			lastMenu.setVisible(false);
		 
	}

	public void mouseClicked(MouseEvent e) {
		int index = viewList.locationToIndex(e.getPoint()); 

		if (index == -1)
			return;

		ListItem item = ((ListItem) viewList.getModel().getElementAt(index));

		clickPoint = e.getPoint();

		if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON3) {

			if (lastMenu != null)
				lastMenu.setVisible(false);
			lastMenu = null;
			final JPopupMenu menu = item.getPopupMenu();

			if (menu != null) {
				menu.setLocation(e.getLocationOnScreen());
				menu.setVisible(true);
				lastMenu = menu;
			}

			return;
		}

		if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
			item.doubleClick();
		}
	}
	 

}
